﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace POSPlus.BTxmlWriter
{
    public class TreeBuilder
    {
        public Tree<String> NodeTree { get; private set; }
        public TreeToXmlConverter Converter { get; private set; }
        public String XMLLocation { get; private set; }
        public Boolean PrintPreview { get; private set; }

        public TreeBuilder(String xmlLocation)
        {
			PrintPreview = false;
            NodeTree = new Tree<string>(new Node<string>("XMLScript", ""));
            NodeTree.AddAttribute("/XMLScript", "Version", "2.0");

            NodeTree.AddNode("/XMLScript", new Node<String>("Command", ""));
            NodeTree.AddAttribute("/XMLScript/Command", "Name", "");

            NodeTree.AddNode("/XMLScript/Command", new Node<string>("Print", ""));

            NodeTree.AddNode("/XMLScript/Command/Print", new Node<string>("Format", ""));

            NodeTree.AddNode("/XMLScript/Command/Print", new Node<string>("RecordSet", ""));
            NodeTree.AddAttribute("/XMLScript/Command/Print/RecordSet", "Name", "Text File 1");
            NodeTree.AddAttribute("/XMLScript/Command/Print/RecordSet", "Type", "btTextFile");

            NodeTree.AddNode("/XMLScript/Command/Print/RecordSet", new Node<String>("Delimitation", "btDelimQuoteAndComma"));

            NodeTree.AddNode("/XMLScript/Command/Print/RecordSet", new Node<string>("UseFieldNamesFromFirstRecord", "true"));

            NodeTree.AddNode("/XMLScript/Command/Print/RecordSet", new Node<string>("TextData", ""));

            Converter = new TreeToXmlConverter(NodeTree, new System.Xml.XmlDocument());
            Converter.Convert();
            Converter.WriteToFile(xmlLocation);
            XMLLocation = xmlLocation;
        }

        public TreeBuilder(String xmlLocation, bool printPreview)
        {
            if (printPreview)
            {
                NodeTree = new Tree<string>(new Node<string>("XMLScript", ""));
                NodeTree.AddAttribute("/XMLScript", "Version", "2.0");

                NodeTree.AddNode("/XMLScript", new Node<String>("Command", ""));
                NodeTree.AddAttribute("/XMLScript/Command", "Name", "");

                NodeTree.AddNode("/XMLScript/Command", new Node<string>("ExportPrintPreviewToImage", ""));
                NodeTree.AddAttribute("/XMLScript/Command/ExportPrintPreviewToImage", "ReturnImageInResponse", "false");
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("BackgroundColor", "16777215"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("Colors", "btColors24Bit"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("DPI", "btResolutionScreen"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("FileNameTemplate", "Preview_Page_%PageNumber%.jpg"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("Folder", ConfigurationManager.AppSettings["PrintPreviewFolder"]));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("ImageFormatType", "JPG"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("IncludeBorder", "true"));
                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<String>("Overwrite", "true"));

                NodeTree.AddNode("/XMLScript/Command/ExportPrintPreviewToImage", new Node<string>("Format", ""));


                Converter = new TreeToXmlConverter(NodeTree, new System.Xml.XmlDocument());
                Converter.Convert();
                Converter.WriteToFile(xmlLocation);
                XMLLocation = xmlLocation;
            }
            PrintPreview = printPreview;
        }
        /// <summary>
        /// Appends the given string on to the TextData of this tree.
        /// </summary>
        /// <param name="line"></param>
        public void AppendTextData(String line)
        {
            Converter.LoadAndModify(XMLLocation, "/XMLScript/Command/Print/RecordSet/TextData", line);
            //Converter.WriteToFile(XMLLocation);
        }

        public void ClipLastNewLine()
        {
            Converter.LoadAndClip(XMLLocation, "/XMLScript/Command/Print/RecordSet/TextData");
        }
        /// <summary>
        /// This is the printer job name to identify the print job later.  The XML Attribute Name under the Command
        /// section is set when JobName is.
        /// </summary>
        public String JobName
        {
            get
            {
                return NodeTree.GetNode("/XMLScript/Command").Attributes["Name"];   
            }

            set
            {
                NodeTree.GetNode("/XMLScript/Command").Attributes["Name"] = value;
                Converter.Convert();
                Converter.WriteToFile(XMLLocation);
            }
        }

        /// <summary>
        /// LabelLocation is the location of the *.btw file needed to be printed.  When this is set the 
        /// XML InnerValue is set as well.
        /// </summary>
        public String LabelLocation
        {
            get
            {
                if (PrintPreview) return NodeTree.GetNode("/XMLScript/Command/ExportPrintPreviewToImage/Format").Value;
                else return NodeTree.GetNode("/XMLScript/Command/Print/Format").Value;
            }

            set
            {
                if (PrintPreview) NodeTree.GetNode("/XMLScript/Command/ExportPrintPreviewToImage/Format").SetInnerValue(value);
                else NodeTree.GetNode("/XMLScript/Command/Print/Format").SetInnerValue(value);
                Converter.Convert();
                Converter.WriteToFile(XMLLocation);
            }
        }
    }
}
